#include "head.h"

// 初始数线程池
int pool_init(pool_t *pool){
    // 检测返回值
    int ret;

    // 读取配置文件拿到线程个数
    char thread_num[3] = {0};
    ret = getParameter("thread_num",thread_num);
    ERROR_CHECK(ret,-1,"getParameter");
    pool->thread_num = atoi(thread_num);
    
    // 初始化锁和条件变量
    ret = pthread_mutex_init(&pool->lock,NULL);
    ERROR_CHECK(ret,-1,"pthread_mutex_init");
    ret = pthread_cond_init(&pool->cond,NULL);
    ERROR_CHECK(ret,-1,"pthread_cond_init");

    // 初始化退出标志位
    pool->exit_flag = 0;

    // 初始化子线程并且拿到id号
    pool->thread_id = (pthread_t *)malloc(sizeof(pthread_t) * pool->thread_num);
    for (int i = 0; i < pool->thread_num; i++){
        ret = pthread_create(&pool->thread_id[i],NULL,thread_main,(void *)pool);
        ERROR_CHECK(ret,-1,"pthread_create");
    }
    return 0;
}
